package com.autonomous_printer.controller;

import com.autonomous_printer.pojo.Order;
import com.autonomous_printer.service.PayService;
import com.autonomous_printer.service.PrintService;
import com.autonomous_printer.util.OrderUtil;
import com.autonomous_printer.util.PrinterStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class PayController {

    @Autowired
    OrderUtil orderUtil;

    @Autowired
    PayService payService;

    @Autowired
    PrintService printService;

    @GetMapping("/pay")
    public String payPage(@RequestParam("id") int id, Model model) {
        if (!orderUtil.contains(id)) {
            return "index";
        }
        model.addAttribute("link", orderUtil.get(id));
        return "pay/payPage";
    }

    @ResponseBody
    @PostMapping("/queryPay")
    public String queryPay(@RequestParam("id") int id) {
        if (!orderUtil.contains(id)) {
            return "failed";
        }
        String queryPay = payService.queryPay(id);
        if ("success".equals(queryPay)) {
            printService.print(id, 1);
        }
        return queryPay;
    }

}
